//
//  SingerNameViewController.m
//  海角音乐
//
//  Created by dllo on 15/8/14.
//  Copyright (c) 2015年 杨元石. All rights reserved.
//

#import "SingerNameViewController.h"

@interface SingerNameViewController ()
{
 
}
@end

@implementation SingerNameViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor blackColor];
    
    CGFloat title_width = SCREEN_WIDTH*0.533333; // 200
    CGFloat title_height = SCREEN_WIDTH*0.08; // 30
    self.TitleView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, title_width, title_height)];
    self.TitleView.textColor = [UIColor whiteColor];
    self.TitleView.textAlignment = NSTextAlignmentCenter;
    self.TitleView.text = self.singerModel.singer_name;;
    self.navigationItem.titleView = self.TitleView;
    
    
    _singerNameArr = [[NSMutableArray alloc] init];   
    
    [self setAutomaticallyAdjustsScrollViewInsets:NO];
    
    _normalView = [[NormalView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT)];
    _normalView.tableView.dataSource = self;
    _normalView.tableView.delegate = self;    
    _normalView.tableView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:_normalView];
    [_normalView release];
    
    // 小菊花
    HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    HUD.removeFromSuperViewOnHide = YES;
    HUD.labelText = @"loading...";
    [self.view addSubview:HUD];
    HUD.mode = MBProgressHUDModeIndeterminate;
    
    // 状态的Bar
    BarPlayer = [DanLiPlayer shareHandleDanLiPlayer:CGRectZero];
    [self.view addSubview:BarPlayer];
    
    // 单例播放器
    Legacy = [SinglePlayer shareHandleSinglePlayer];
    
    
    [self addFooter];
    [self getSingerInfo];
    [self getDataForSingerName:1];
    self.Nextpage = 2;
    

    
    
    
    
    // Do any additional setup after loading the view.
}




#pragma mark --上拉加载更多
- (void)addFooter
{
    
    __block  SingerNameViewController *singerVc = self;
    [_normalView.tableView addFooterWithCallback:^{
        
        NSLog(@"vc.Nextpage =====  %ld", (long)singerVc.Nextpage);
        [self getDataForSingerName:self.Nextpage];
        self.Nextpage++;
    }];
}

// 获取歌手信息
- (void)getSingerInfo
{
    
    NSString *url_one = @"http://api.dongting.com/song/singer/";    
    NSString *url_string = [NSString stringWithFormat:@"%@%@?detail=true", url_one, self.singerModel.singer_id];

    [AFN urlString:url_string getAFNdata:^(id responseObject) {
        
        NSDictionary *dataDic = [responseObject objectForKey:@"data"];
        Singer_InfoModel *singer_info = [[Singer_InfoModel alloc] init];
        [singer_info setValuesForKeysWithDictionary:dataDic];
        [_normalView getURLForImage:singer_info.picUrl textForLabel:nil];
        
    }];
    
    
}
// 获取 歌曲信息
- (void)getDataForSingerName:(NSInteger)page
{
    NSString *url_one = @"http://api.dongting.com/song/singer/";
    NSString *page_three = [NSString stringWithFormat:@"/songs?page=%ld", page];
    NSString *url_string = [NSString stringWithFormat:@"%@%@%@", url_one, self.singerModel.singer_id, page_three];

    [AFN urlString:url_string getAFNdata:^(id responseObject) {
        
        NSArray *dataArr = [responseObject objectForKey:@"data"];
        for (NSDictionary *dic in dataArr) {
            ReDetailModel *singerName = [[ReDetailModel alloc] init];
            [singerName setValuesForKeysWithDictionary:dic];
            [_singerNameArr addObject:singerName];
        }
        HUD.hidden = YES;
        [_normalView.tableView reloadData];
        [_normalView.tableView footerEndRefreshing];
        
    }];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _singerNameArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellId = @"cell";
    ReDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (cell == NO) {
        cell = [[ReDetailTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
    }
    // 越界保护
    if (_singerNameArr.count > indexPath.row) {
        ReDetailModel *singerName = [_singerNameArr objectAtIndex:indexPath.row];
        [cell getDataForCell:singerName];
    }
    cell.textLabel.text = [NSString stringWithFormat:@"%ld", indexPath.row+1];
    cell.textLabel.textColor =[UIColor whiteColor];
    return cell;    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return SCREEN_WIDTH*0.2133333;
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    ReDetailModel *reDetailmodel = [_singerNameArr objectAtIndex:indexPath.row];
    BarPlayer.reDetailModel = reDetailmodel;
    BarPlayer.dataArr = _singerNameArr;
    BarPlayer.index = indexPath.row;
    [self playeMusicWithModel:reDetailmodel index:indexPath.row];
}


// 播放方法
- (void)playeMusicWithModel:(ReDetailModel *)model index:(NSInteger)index
{
    if (model.audition_list.count > 1) {
        NSDictionary *dic = [model.audition_list objectAtIndex:1];
        NSString *url = [dic objectForKey:@"url"];
        [Legacy play:url];
        
    } else {
        NSDictionary *dic = [model.audition_list objectAtIndex:0];
        NSString *url= [dic objectForKey:@"url"];
        [Legacy play:url];
    }
    
}







- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
